package com.hlw.hr_man.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hlw.hr_man.Model.Overtime;
import com.hlw.hr_man.service.OvertimeService;
import com.hlw.hr_man.serviceImpl.OvertimeServiceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @ClassName: SearchEmployeeOvertimeController
 * @Description: 处理搜索员工加班信息的请求
 * @author 唐明
 * @date 2016年9月20日 上午10:20:16
 *
 */
public class SearchEmployeeOvertimeController extends HttpServlet {

	/**
	 * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
	 */
	private static final long serialVersionUID = 1L;
	
	OvertimeService otService = new OvertimeServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		//获得搜索类型
		String searchType = req.getParameter("searchType");
		//获得搜索内容
		String searchContent = req.getParameter("searchContent");
		// 获得当前页码
		int page = Integer.parseInt(req.getParameter("page"));
		// 获得每页行数
		int pageSize = Integer.parseInt(req.getParameter("rows"));
		// 计算开始页码
		int start = (page - 1) * pageSize;
		// 获得当前用户的加班列表
		List<Overtime> ots = otService.getEmployeeOvertimeBySearch(searchType, searchContent, start, pageSize);
		// 获得当前用户加班记录总数
		int ot_count = otService.getEmployeeOvertimeCount(searchType, searchContent);
		// 将加班记录以json格式存放
		JSONObject currentEmpOts = new JSONObject();
		currentEmpOts.put("total", ot_count);
		currentEmpOts.put("rows", JSONArray.fromObject(ots));
		// 将数据传回页面
		PrintWriter pw = resp.getWriter();
		pw.write(currentEmpOts.toString());
	}
}
